package linkedlist;

public class ListNode {

    public int val;
    public ListNode next;
    public int size;

    public ListNode(int x){
        val = x;
        size++;
    }

    public ListNode(int[] arr){
        this.val = arr[0];

        ListNode current = this;
        for (int i = 1; i < arr.length; i++) {
            current.next = new ListNode(arr[i]);
            current = current.next;
            size++;
        }
    }

    public void loop(int size){

        ListNode current = this;
        for(int i = 0 ; i < size ; i ++){
            System.out.println(current.val);
            current = current.next;

        }
    }

    //递归添加元素
    public void add(int val){
        if(this == null){
            ListNode node = new ListNode(val);
            size++;
        }else {
            add(this,val);
        }
    }

    //返回当前节点
    private void add(ListNode node,int val){

        if(node.next == null){
            ListNode addNode = new ListNode(val);
            addNode.next = null;
            node.next = addNode;
            size++;
            return;
        }

        add(node.next,val);


    }



}
